﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Text;
using System.IO;
using System.Diagnostics;
public partial class Admin_adminProduct : basePage
{
    int pagesize = 20;
    public int num = 0;
    productBll pBll = new productBll();
    productClassBll pcBll = new productClassBll();
    string urlState = string.Empty;
    private static readonly object objSync = new object();
    public string input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=1' />";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            if (Request.QueryString["action"] != null)
            {

                 
                string action = Request.QueryString["action"].ToString();
                string id = Request["id"].ToString();
                switch (action.Trim())
                {
                    case "delAll":
                        delAll(id);
                        break;
                    case "editAll":
                        editAll(id);
                        break;
                    case "MoveAll":
                        moveUpdate(id);
                        break;
                    case "bast":
                        isBast(id);
                        break;
                    case "new":
                        IsNew(id);
                        break;
                    case "index":
                        isIndex(id);
                        break;
                    case "can":
                        isCan(id);
                        break;
                    default:
                        // instance = accInstance;
                        break;
                }               
            }
            DataTable dt = pcBll.GetDataTable();
            ViewState["dt"] = dt;
            bindProduct();
            //sw.Stop();
            //Response.Write(sw.ElapsedMilliseconds);
       
        }
    }

    #region 显示产品   
    public void bindProduct()
    {
        if (Request.QueryString["CurrentIndex"] != null)
        {
            _currentIndex = Convert.ToInt32(Request.QueryString["CurrentIndex"]);
        }
        if (Request.QueryString["ClassId"] != null)
        {
            _classid = Convert.ToInt32(Request.QueryString["ClassId"]);
        }
        if (Request.QueryString["Key"] != null)
        {
            _key = Request.QueryString["Key"].ToString();
        }
        DataTable dt = pBll.pagingForProduct(_currentIndex, pagesize,_classid, _key);
        if (dt.Rows.Count == 0 && _currentIndex > 1)
        {
            _currentIndex = _currentIndex - 1;
            dt = pBll.pagingForProduct(_currentIndex, pagesize,_classid, _key);
        }
        input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=" + _currentIndex + "&ClassId=" + _classid + "&Key=" + _key + "'/>";
        num = _currentIndex * pagesize - pagesize;
        this.Repeater1.DataSource = dt;
        this.Repeater1.DataBind();

        //StringWriter sw = new StringWriter();
        //HtmlTextWriter htw = new HtmlTextWriter(sw);
        //Repeater1.RenderControl(htw);
        
        //Response.Write(sw.ToString());
        //Response.End();
    }
    #endregion
    public void bindProduct1()
    {
        if (Request.QueryString["CurrentIndex"] != null)
        {
            _currentIndex = Convert.ToInt32(Request.QueryString["CurrentIndex"]);
        }
        if (Request.QueryString["ClassId"] != null)
        {
            _classid = Convert.ToInt32(Request.QueryString["ClassId"]);
        }
        if (Request.QueryString["Key"] != null)
        {
            _key = Request.QueryString["Key"].ToString();
        }
        DataTable dt = pBll.pagingForProduct(_currentIndex, pagesize, _classid, _key);
        if (dt.Rows.Count == 0 && _currentIndex > 1)
        {
            _currentIndex = _currentIndex - 1;
            dt = pBll.pagingForProduct(_currentIndex, pagesize, _classid, _key);
        }
        input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=" + _currentIndex + "&ClassId=" + _classid + "&Key=" + _key + "'/>";
        num = _currentIndex * pagesize - pagesize;
        this.Repeater1.DataSource = dt;
        this.Repeater1.DataBind();
    }
 
    #region 状态维护属性
    private int _currentIndex=1;
    private int _classid = 0;
    private string _key = "";
    #endregion

    #region 分页
    public void htmlPaging()
    {
        string str = null;
        int count = 0;
        count = pBll.getRecordCount(_classid,_key);
        int pcount = count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
        str = "页次：" + _currentIndex + "/" + pcount + "  每页" + pagesize + " 总收录数据" + count + "条";
        str += HtmlControls.pagIng(_currentIndex, pcount, "adminProduct.aspx?ClassId=" + _classid + "&key=" + _key + "&CurrentIndex", "pageaa", "pagecurrent");
        Response.Write(str);
    }
    #endregion

    #region AJAX批量删除,SQL拼接需要过滤的地方
    public void delAll(string id)
    {
        if (id.Contains(","))
        {
            string[] idParam = id.Split(',');
            string[] pid = new string[idParam.Length];
            for (int i = 0; i < idParam.Length; i++)
            {
                pid[i] = idParam[i];
            }
            for (int i = 0; i < pid.Length; i++)
            {
                product pModel= pBll.GetModel(Convert.ToInt32(pid[i]));
                if (pModel.Productmodel!="")
                {
                    string path = Server.MapPath("~") + "/UploadFiles/" + pModel.Productmodel;
                    if(Directory.Exists(path))
                    {
                        Directory.Delete(path, true);
                    }
                }               
            }       
            if (pBll.Delete(pid))
            {
                       
            }
        }
        else
        {

            product pModel = pBll.GetModel(Convert.ToInt32(id));
            if (pBll.Delete(Convert.ToInt32(id)))
            {
                if (pModel.Productmodel != "")
                {
                    string path =@"UploadFiles\productUpload\" + pModel.Productmodel;
                    DirFile.DeleteDir(path, true);
                }
            }
        }
        GC.ReRegisterForFinalize(this);
    }
    #endregion

    #region "AJAX批量修改,"
    public void editAll(string id)
    {

        try
        {
            if (id.Contains(","))
            {
                lock (objSync)
                {
                    string[] idParam = id.Split(',');
                    for (int i = 0; i <= idParam.Length - 1; i++)
                    {
                        product p = new product();
                        p.productId = Convert.ToInt32(idParam[i]);
                        if (!CommUtil.IsNumStr(Request.Form["pSort" + idParam[i] + ""].ToString().Trim()) || Request.Form["pSort" + idParam[i] + ""].ToString().Trim() == "")
                        {
                            return;
                        }
                        if (!CommUtil.IsNumStr(Request.Form["pview" + idParam[i] + ""].ToString().Trim()) || Request.Form["pview" + idParam[i] + ""].ToString().Trim() == "")
                        {
                            return;
                        }
                        p.pSort = Convert.ToInt32(Request.Form["pSort" + idParam[i] + ""].ToString());
                        p.Views = Convert.ToInt32(Request.Form["pview" + idParam[i] + ""].ToString());
                        p.IsNew = Convert.ToBoolean(Request.Form["IsNew" + idParam[i] + ""].ToString());
                        p.isBast = Convert.ToBoolean(Request.Form["isBast" + idParam[i] + ""].ToString());
                        p.isIndex = Convert.ToBoolean(Request.Form["isIndex" + idParam[i] + ""].ToString());
                        p.isCan = Convert.ToBoolean(Request.Form["isCan" + idParam[i] + ""].ToString());
                        p.Relationmark = Convert.ToInt32(Request.Form["mark"+idParam[i]+""]);
                        p.eDate = DateTime.Now;

                        if (pBll.updateFast(p))
                        {
                        }
                        else
                            return;
                    }
                }

            }
            else
            {
                product p = new product();
                p.productId = Convert.ToInt32(id);
                if (!CommUtil.IsNumStr(Request.Form["pSort" + id + ""].ToString().Trim()) || Request.Form["pSort" + id + ""].ToString().Trim() == "")
                {
                    return;
                }
                if (!CommUtil.IsNumStr(Request.Form["pview" + id + ""].ToString().Trim()) || Request.Form["pview" + id + ""].ToString().Trim() == "")
                {
                    return;
                }
                p.pSort = Convert.ToInt32(Request.Form["pSort" + id + ""].ToString());
                p.Views = Convert.ToInt32(Request.Form["pview" + id + ""].ToString());
                p.IsNew = Convert.ToBoolean(Request.Form["IsNew" + id + ""].ToString());
                p.isBast = Convert.ToBoolean(Request.Form["isBast" + id + ""].ToString());
                p.isIndex = Convert.ToBoolean(Request.Form["isIndex" + id + ""].ToString());
                p.isCan = Convert.ToBoolean(Request.Form["isCan" + id + ""].ToString());
                p.Relationmark = Convert.ToInt32(Request.Form["mark"+id]);
                p.eDate = DateTime.Now;
                if (pBll.updateFast(p))
                {

                    
                }
                else
                    return;
            }
        }
        catch (Exception ex)
        {
            JsUtil.ShowMsg(ex.Message);
        }
        finally
        {

        }

       // Response.Write("1");
        //Response.End();
    }
    #endregion

    #region AJAX批量移动 SQL拼接需要过滤的地方
    public void moveUpdate(string id)
    {

        try
        {
            string pid = Request.QueryString["pid"].ToString();
            if (pid.Contains(","))
            {
                string[] idParam = pid.Split(',');
                string[] pidParam = new string[idParam.Length];
                for (int i = 0; i <= idParam.Length - 1; i++)
                {
                    pidParam[i] = idParam[i];
                }
                if (pBll.Update(pidParam, id))
                {
               
                }
            }
            else
            {
                string[] productid ={pid};
                if (pBll.Update(productid,id))
                {
                    
                   
                }
            }
        }
        catch
        {

        }
        //Response.Write("1");
        //Response.End();
    }
    #endregion

    #region "抛弃服务器控件,自己绑定下拉框"
    public string ProductClassDp(string name)
    {
        StringBuilder str = new StringBuilder();
        #region 提供递归缓存
        ViewState["dt"] = pcBll.GetDataTable();
        #endregion
        str.Append("<select name='selectDp' id='" + name + "' style='width:192px;'>");
        str.Append("<option value='0'>---顶级分类---</option>");
        str.Append(AddItems(0, 0, new StringBuilder()));
        str.Append("</select>");
        return str.ToString();
    }

    public string AddItems(int ParentID, int length, StringBuilder sb)
    {
        DataView dvTree = pcBll.GetDataView(ViewState["dt"] as DataTable, ParentID);
        length++;
        foreach (DataRowView row in dvTree)
        {
            string str = addtab(length);
            sb.AppendFormat("<option value='" + row["pClassId"].ToString() + "'>{0}</option>", str + "|--" + row["pClassName"].ToString());
            AddItems(Convert.ToInt32(row["pClassId"].ToString()), length, sb);
        }
        return sb.ToString();
    }
    private string addtab(int i)
    {
        string mark = "&nbsp;";
        if (i != 1)
        {
            for (int j = 0; j < i - 1; j++)
            {

                mark += "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }
        return mark;
    }
    #endregion

    #region 根据ID号选择产品
    public string chooseProductByClassId(int cid)
    {
    string str = null;
    if (cid == 0)
    {
    str = "<font color='red'>类别未绑定</red>";
    }
    else
    {
    DataView dv = pcBll.GetModel(ViewState["dt"] as DataTable, cid);

    foreach (DataRowView row in dv)
    {
    if(row!=null)
    str = row["pClassName"].ToString();
    else
    str = "<font color='red'>所属类别丢失</red>";
    }

    }
    return str;

    }
    #endregion

    #region 快速推荐
    public void isBast(string id)
    {
       
        bool bo=Convert.ToBoolean(Request.QueryString["bo"]);
        int isid=Convert.ToInt32(Request.QueryString["id"]);
        bool boRealut = pBll.isBast(bo, isid);
        if (boRealut != bo)
        {
            if (boRealut == true)
            {
                Response.Write("已推荐");
            }
            else
            {
                Response.Write("推荐");
            }
        }
        Response.End();

    }
    #endregion

    #region 最新设置
    public void IsNew(string id)
    {

        bool bo = Convert.ToBoolean(Request.QueryString["bo"]);
        int isid = Convert.ToInt32(Request.QueryString["id"]);
        bool boRealut = pBll.IsNew(bo, isid);
        if (boRealut != bo)
        {
            if (boRealut == true)
            {
                Response.Write("已最新");
            }
            else
            {
                Response.Write("最新");
            }
        }
        Response.End();

    }
    #endregion

    #region 首页推荐
    public void isIndex(string id)
    {

        bool bo = Convert.ToBoolean(Request.QueryString["bo"]);
        int isid = Convert.ToInt32(Request.QueryString["id"]);
        bool boRealut = pBll.isIndex(bo, isid);
        if (boRealut != bo)
        {
            if (boRealut == true)
            {
                Response.Write("已首页推荐");
            }
            else
            {
                Response.Write("首页推荐");
            }
        }
        Response.End();

    }
    #endregion

    #region 快速屏蔽
    public void isCan(string id)
    {

        bool bo = Convert.ToBoolean(Request.QueryString["bo"]);
        int isid = Convert.ToInt32(Request.QueryString["id"]);
        bool boRealut = pBll.isCan(bo, isid);
        if (boRealut != bo)
        {
            if (boRealut == true)
            {
                
                Response.Write("已屏蔽");
            }
            else
            {
                Response.Write("屏蔽");
            }
        }
        Response.End();

    }
    #endregion
 
}
